var screenW=document.documentElement.clientWidth;

//大图预览
function zoomImg(photoUrl) {
    $("#zoom-img > img").attr("src", photoUrl)
    $("body").attr("style", "overflow: hidden;")
    $("#zoom-img-wrapper").show();
    $("#zoom-img").show(500);
}
//判断不同分辨率设备显示不一样的大小
if(screenW > 768){
    $("#zoom-img").css({"width":"45%","height":"55%","margin":"10% 27%"});
}

//旋转图片
var angle = 0;
$("#ic-rotate").bind("click", function (event) {
    event.stopPropagation();
    $("#ic-photo").rotate({
        angle: angle,
        animateTo: angle + 90,
        duration: 300,
        callback: function () { angle += 90 }
    });
});

$("#zoom-img-wrapper").bind("click", function (event) {
    event.stopPropagation();
})
$("#zoom-img-wrapper .closed ").bind("click", function (event) {
    event.stopPropagation();
    $("#zoom-img").hide(500)
    setTimeout(() => {
        $("#zoom-img-wrapper").hide()
    $("body").removeAttr("style")
}, 500);
    $("#ic-photo").rotate({
        angle: 0,
        animateTo: 0,
        duration: 300,
        callback: function () { angle += 0 }
    });
});
//退出登录
function logout() {
    $.ajax({
        url: '/csfy/dologout.do',
        method: 'POST',
        success: function (res) {
            if (JSON.parse(res).code == 200) {
                window.location.href = "/csfy/index.do"
            }
        }
    })
}

//获取当前登录用户信息
$(document).ready(function () {
    $.ajax({
        method: 'POST',
        url: '/csfy/intf.do',
        data: { method: 'getLoginUserInfo' },
        success: res => {
        if (res.code == 10086) {
//                        window.location.href = "/csfy/index.do"
        alert("请重新登录！");
    } else {
        if (res.code == 200) {
            $(".username").html(res.rows.userName);
        }
    }
}
})
})
function getJsonFromUrl() {
    var query = location.search.substr(1);
    var result = {};
    query.split("&").forEach(function (part) {
        var item = part.split("=");
        result[item[0]] = decodeURIComponent(item[1]);
    });
    return result;
}

var id = getJsonFromUrl().id;
if (!id) {
    window.location.href = "/csfy/static/manage-sys/coupon-review.html"
} else {
    //优惠类型
    function getPersonType(type) {
        type = parseInt(type);
        if (isNaN(type)) {
            return '其他免费类型';
        }
        if (type === 1) {
            return '本人为长沙户口';
        } else if (type === 2) {
            return '配偶为长沙户口';
        } else if (type === 3) {
            return '长沙居住证';
        } else if (type === 4) {
            return '军人及军人配偶';
        } else if (type === 5) {
            return '其他免费类型';
        }
    }

    function agree(couponNum) {
        console.log(couponNum)
        var isAgree = confirm('确认同意领取？');
        if (isAgree) {
            $.ajax({
                url: '/csfy/intf.do',
                method: 'POST',
                data: { method: 'updateCouponInfo', couponNum: couponNum, status: '1' },
                success: function () {
                    alert('操作完成');
                    window.location.reload();
                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }
    }

    function disAgree(couponNum) {
        console.log("不同意")
        var isAgree = confirm('确认不同意领取？');
        if (isAgree) {
            $.ajax({
                url: '/csfy/intf.do',
                method: 'POST',
                data: { method: 'updateCouponInfo', couponNum: couponNum, status: '-1' },
                success: function () {
                    alert('操作完成');
                    window.location.reload();
                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }
    }

    //TODO 获取详情信息
    $.ajax({
        url: '/csfy/intf.do',
        method: 'POST',
        data: { method: 'getDetailPresonInfo', id: id },
        success: function (res) {
            if (res.code == 200 && res.rows) {
                $("#person-address").html(res.rows.address);
                $("#person-type").html(getPersonType(res.rows.presonType));
                var data = { rows: [] };
                if (res.rows.cardFrontImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.cardFrontImgUrl,
                        name: '身份证'
                    })
                }
                if (res.rows.marriageImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.marriageImgUrl,
                        name: '结婚证'
                    })
                }
                if (res.rows.cardMateImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.cardMateImgUrl,
                        name: '丈夫身份证'
                    })
                }
                if (res.rows.officerImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.officerImgUrl,
                        name: '军官证'
                    })
                }
                if (res.rows.residencePermitImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.residencePermitImgUrl,
                        name: '居住证'
                    })
                }
                if (res.rows.familyRegisterImgUrl) {
                    data.rows.push({
                        photoUrl: res.rows.familyRegisterImgUrl,
                        name: '户口本'
                    })
                }
                $(".idcard-list").html(template('photo-list', data));

                var couponList = '';
                var data1 = { rows: [] }
                if (res.rows.couponInfos && res.rows.couponInfos.length && res.rows.couponInfos.length > 0) {
                    res.rows.couponInfos.map(item => {
                        if(item.status == 0){
                        var couponNum = item.couponNum;
                        couponList += '<div class="coupon-item"><div class="name">' + item.couponName + '<button type="button" class="btn btn-primary" style="margin: 0 40px" onclick="agree('+"'"+couponNum+"'"+')">同意领取</button><button type="button" class="btn btn-default" onclick="disAgree('+"'"+couponNum+"'"+')">不同意</button>' + '</div><div class="desc">' + item.couponDesc + '</div></div>'
                    }
                })
                }
                $(".coupon-list").html(couponList);
            } else {
                alert('获取用户免费券信息失败，请稍后重试');
                window.location.href = "/csfy/static/manage-sys/coupon-review.html"
            }
        },
        error: function (msg) {
            alert(msg);
            window.location.href = "/csfy/static/manage-sys/coupon-review.html"
        }
    })
}